<?php

defined('SYSPATH') or die('No direct access allowed.');

class Controller_User extends Controller_Site {
    public function action_login(){
        if($this->request->method()=='POST'){
            $user = ORM::factory('user')->where('username', '=', $this->request->post('username'))->where('password', '=', md5($this->request->post('password')))->find();
            Session::instance()->set('user_id', $user->id);
            Session::instance()->set('town_id', ORM::factory('usertown')->where('user_id', '=', $user->id)->where('hq', '=', 1)->find()->town_id);
            $this->calculateresources();
            $this->request->redirect('');
        }
    }
    
    public function action_logout(){
        Session::instance()->destroy();
        $this->request->redirect('');
    }
    
    public function action_register(){
        if($this->request->method() == 'POST') {
            try {
                $user = ORM::factory('user');
                $user->username = $this->request->post('username');
                $user->password = md5($this->request->post('password'));
                $user->email = $this->request->post('email');
                $user->last_time = time();
                $user->save();
                
                //generate town
                $this->generate_town($user->id, TRUE);
                $town_id = ORM::factory('towns')->where('user_id', '=', $user->id)->find()->id;
                
                //initial resources
                $this->add_resources($user->id, $town_id);
                           
                //logging in
                Session::instance()->set('user_id', $user->id);
                Session::instance()->set('town_id', $town_id);
                
                $this->request->redirect('');
            }
            catch (ORM_Validation_Exception $e) {
                //$this->set('errors', $e->errors('models'));
            }
        }
    }
    
    public function generate_town($user_id, $first = FALSE){
        //create town
        $town = ORM::factory('towns');
        $town->user_id = $user_id;
        $town->save();
        
        //user <-> town
        $user_town = ORM::factory('usertown');
        $user_town->user_id = $user_id;
        $user_town->town_id = $town->id;
        if($first){
            $user_town->hq = 1;
        }
        $user_town->save();
        
        //town <-> buildings
        $place_index=1;
        
        //store
        $building = ORM::factory('townbuildings');
        $building->town_id = $town->id;
        $building->place_id = $place_index;
        $place_index++;
        $building->building_id = 2;
        $building->save();
        
        //sawmill level 1
        $building = ORM::factory('townbuildings');
        $building->town_id = $town->id;
        $building->place_id = $place_index;
        $place_index++;
        $building->building_id = 5;
        $building->save();
        
        //farm level 1
        $building = ORM::factory('townbuildings');
        $building->town_id = $town->id;
        $building->place_id = $place_index;
        $place_index++;
        $building->building_id = 8;
        $building->save();
        
        //quarry level 1
        $building = ORM::factory('townbuildings');
        $building->town_id = $town->id;
        $building->place_id = $place_index;
        $place_index++;
        $building->building_id = 11;
        $building->save();
        
        //goldmine level 1
        $building = ORM::factory('townbuildings');
        $building->town_id = $town->id;
        $building->place_id = $place_index;
        $place_index++;
        $building->building_id = 14;
        $building->save();
        
        //other blank
        for($place_index; $place_index<11; $place_index++){
            $building = ORM::factory('townbuildings');
            $building->town_id = $town->id;
            $building->place_id = $place_index;
            $building->building_id = 1;
            $building->save();
        }
    }
    
    public function add_resources($user_id, $town_id){
        for($product_id=1; $product_id<5; $product_id++){
            $res = ORM::factory('userresource');
            $res->user_id = $user_id;
            $res->town_id = $town_id;
            $res->product_id = $product_id;
            $res->quantity = 500;
            $res->save();
        }
    }

    public function action_updatetime(){
        $this->calculateresources();
        $user = ORM::factory('user', SESSION::instance()->get('user_id'));
        $user->last_time = time();
        $user->save();
    }
    
    public function calculateresources($from_time=NULL, $to_time=NULL){
        //esetleges függvény lockolás kellhet
        $user_id = SESSION::instance()->get('user_id');
        if($from_time!=NULL){
            $ft = $from_time;
        }else{
            $ft = ORM::factory('user', $user_id)->last_time;
        }
        if($to_time!=NULL){
            $tt = $to_time;
        }else{
            $tt = time();
        }
        $lt = $ft;
        
        //towns
        $towns = ORM::factory('usertown')->where('user_id', '=', $user_id)->find_all();
        //town->buildings
        foreach($towns as $town){
            $buildings = ORM::factory('townbuildings')->where('town_id', '=', $town->town_id)->find_all();
            foreach($buildings as $building){
                $produce = ORM::factory('buildingproduce')->where('building_id', '=', $building->building_id)->find_all();
                foreach($produce as $p){
                    $user = ORM::factory('userresource')->where('user_id', '=', SESSION::instance()->get('user_id'))->where('town_id', '=', SESSION::instance()->get('town_id'))->where('product_id', '=', $p->product_id)->find();
                    $user->quantity +=(($tt-$lt)*($p->quantity/3600));
                    $user->save();
                    $lt = $ft;
                }
            }
        }
    }
    
}